require(['./config'],()=>{
	require(['template','header','footer','bootstrap'],(template,header)=>{
		class ShoppingCart{
			constructor(arg) {
			    this.render()
				//有商品才需要执行
				if(this.cart){
					
					this.setAllCheckStatus()
					this.calcTotaMoney()
					this.changeCount()
					this.checkChange()
					this.changeAllCheck()
					// this.delList()
					this.discount()
					this.distbOriginPrice()
					// this.render()
				}
				
			}
			
			//判断locaStorage有没有值
			render(){
				let cart=localStorage.getItem('cart')
				// console.log(cart)
				if(cart){
					this.cart=JSON.parse(cart)
					$('#shoplist').html(template('shoplistTemlate',{cart:this.cart}))
				}else{
					$('#uncartlist').show();
					$('#cartlist').hide()
				}
			}
			
			//改变单选状态
			checkChange(){
				const _this=this
				$('.checks').on('change',function(){
					
					//获取当前数据的id
					const id=$(this).parents('ul').data('id')
					// console.log(id)
					//根据id去修改id
					_this.cart=_this.cart.map(shop=>{
						if(shop.id === id){
							shop.check=$(this).prop('checked')
						}
						return shop
						//存localStorage
						
					})
					localStorage.setItem('cart',JSON.stringify(_this.cart))
					//重新设置全选
					_this.setAllCheckStatus()
					//重新设置总价
					_this.calcTotaMoney()
					_this.discount()
					_this.distbOriginPrice()
				})
			}
			//全选按钮改变全部的check状态
			changeAllCheck(){
				const _this=this
				$('#allcheck').on('change',function(){
					_this.cart=_this.cart.map(shop=>{
						shop.check=$(this).prop('checked')
						return shop
					})
					localStorage.setItem('cart',JSON.stringify(_this.cart))
					_this.calcTotaMoney()
					_this.setAllCheckStatus()
					_this.render()
					_this.discount()
					_this.distbOriginPrice()
				})
			}
			//全部选中
			setAllCheckStatus(){
				const isAllcheck=this.cart.every(shop=>shop.check)
				$('#allcheck').prop('checked',isAllcheck)
			}
			//计算总价
			calcTotaMoney(){
			const allMoney=	this.cart.reduce((money,shop)=>{
					 if(shop.check)money +=shop.price*shop.count
					//无论条件是否满足都要return
					return money
				},0)
				$('#paymoney').html(allMoney.toFixed(2))
			}
			
			// 计算优惠
			discount(){
				const discountMoney=this.cart.reduce((money,shop)=>{
						 if(shop.check)money +=shop.tbOriginPrice*shop.count
						//无论条件是否满足都要return
						return money
					},0)
				$('#orgmoney').html(discountMoney.toFixed(2))
			}
			//计算原价
			distbOriginPrice(){
				const OriginPrice=this.cart.reduce((money,shop)=>{
						 if(shop.check)money +=(shop.tbOriginPrice-shop.price)*shop.count
						//无论条件是否满足都要return
						return money
					},0)
					$('#discount ').html(OriginPrice.toFixed(2))
			}
			//加减按钮改变数量
			changeCount(){
				const  _this=this
				$('#shoplist').on('click','button',function(){
					if($(this).hasClass('amountup')){
						let middle=$(this).parent().parent().children('input').val()
						middle++
						$(this).parent().parent().children('input').html(middle)
						const id=$(this).parents('ul').data('id')//获取当前点击事件父级元素的id
						//使用map的方法遍历cart
						_this.cart=_this.cart.map(shop=>{
							//匹配与id相等的
							if(shop.id=== id){
									shop.count++							
							}
							return shop
						})
						localStorage.setItem('cart',JSON.stringify(_this.cart))
						_this.setAllCheckStatus()
						//重新设置总价
						_this.calcTotaMoney()
						header.cartCount()
						_this.discount()
						_this.distbOriginPrice()
						_this.render()
					}else if($(this).hasClass('amountdownn')){
						const id=$(this).parents('ul').data('id')
						_this.cart=_this.cart.map(shop=>{
							if(shop.id=== id){
								if(shop.count>1){
									shop.count--
								}	
							}
							return shop
						})
						localStorage.setItem('cart',JSON.stringify(_this.cart))
						_this.setAllCheckStatus()
						//重新设置总价
						_this.calcTotaMoney()
						header.cartCount()
						_this.discount()
						_this.distbOriginPrice()
						_this.render()
					}else if($(this).hasClass('delbtn')){//删除功能
							const id=$(this).parents('ul').data('id')
							console.log(id)
							_this.cart=_this.cart.filter(shop=>{
								if(shop.id!=id) return shop
							})
							console.log(_this.cart.length)
							if(_this.cart.length<1){
								localStorage.removeItem('cart')
							}else{
								localStorage.setItem('cart',JSON.stringify(_this.cart))
							}
							
							_this.setAllCheckStatus()
							//重新设置总价
							_this.calcTotaMoney()
							header.cartCount()
							_this.discount()
							_this.distbOriginPrice()
							_this.render()
					}
				})
			}
			
			
		}
		new ShoppingCart()
	})
})